package com.example.ovulationperiodtracker.view;

import android.text.TextUtils;

import com.example.ovulationperiodtracker.utils.Constants;
import com.example.ovulationperiodtracker.utils.MyLog;
import com.google.gson.Gson;
import com.tencent.mmkv.MMKV;

import java.lang.reflect.Array;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

public class NoteUtils {


    private static String JingLengKey = "JingLengKey";
    private static String PeriodLengthKey = "PeriodLengthKey";
    private static String PinKey = "PinKey";
    private static String HuangTiQiKey = "HuangTiQiKey";
    private static String YuChanQiKey = "YuChanQiKey";
    private static String YuChanQiStartKey = "YuChanQiStartKey";
    private static final MMKV mmkv = MMKV.defaultMMKV();

    private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    private static Gson gson = new Gson();


    public static DayNote getDayNote(LocalDate localDate) {

        String formattedDate = localDate.format(formatter);
        String json = mmkv.decodeString(formattedDate, "");
        if (TextUtils.isEmpty(json)) {
            return new DayNote(formattedDate);
        }
        return gson.fromJson(json, DayNote.class);
    }

    public static void saveDayNote(DayNote dayNote) {
        String json = gson.toJson(dayNote);
        mmkv.encode(dayNote.date, json);
    }

    //获取经期长度
    public static int getJingLength() {
        return mmkv.decodeInt(JingLengKey, 0);
    }

    //保存经期长度
    public static void saveJingLength(int length) {
        mmkv.encode(JingLengKey, length);
    }


    //获取pin
    public static String getPin() {
        return mmkv.decodeString(PinKey, "");
    }

    //保存pin
    public static void savePin(String pin) {
        mmkv.encode(PinKey, pin);
    }

    //获取周期长度
    public static int getPeriodLength() {
        return mmkv.decodeInt(PeriodLengthKey, 28);
    }

    //保存周期长度
    public static void savePeriodLength(int length) {
        mmkv.encode(PeriodLengthKey, length);
    }

    //获取黄体期
    public static int getHuangTiQi() {
        return mmkv.decodeInt(HuangTiQiKey, 14);
    }

    //保存周期长度
    public static void saveHuangTiQi(int length) {
        mmkv.encode(HuangTiQiKey, length);
    }

    //获取预产期
    public static LocalDate getYuChanQi() {
        String[] strings = mmkv.allKeys();
        if(strings==null) return null;

        String s = mmkv.decodeString(YuChanQiKey, "");
        if (TextUtils.isEmpty(s)) {
            return null;
        }
        return LocalDate.parse(s);
    }

    //保存预产期
    public static void saveYuChanQi(LocalDate localDate) {
        if (localDate == null) {
            mmkv.remove(YuChanQiKey);
            mmkv.remove(YuChanQiStartKey);
        } else {
            String formattedDate = localDate.format(formatter);
            mmkv.encode(YuChanQiKey, formattedDate);
            mmkv.encode(YuChanQiStartKey, LocalDate.now().format(formatter));
        }
    }


    //获取预产期设置的日期
    public static LocalDate getYuChanQiStart() {
        String[] strings = mmkv.allKeys();
        if(strings==null) return null;

        String s = mmkv.decodeString(YuChanQiStartKey, "");
        if (TextUtils.isEmpty(s)) {
            return null;
        }
        return LocalDate.parse(s);
    }


    //所有的月经开始的日期
    public static List<LocalDate> getAllJingStart() {

        String[] strings = mmkv.allKeys();
        if(strings==null) return new ArrayList<>();

        List<String> allKeys = Arrays.asList(mmkv.allKeys());
        List<LocalDate> collect = allKeys.stream()
                .filter(key -> key != null && (key.startsWith("1") || key.startsWith("2")))
                .map(key -> LocalDate.parse(key))
                .sorted(Comparator.reverseOrder())
                .map(localDate -> {
                    return getDayNote(localDate);
                })
                .filter(dn -> dn.jingStart == 1)
                .map(dayNote -> {
                    return LocalDate.parse(dayNote.date);
                })
                .collect(Collectors.toList());
        return collect;
    }
    //所有的月经结束的日期
    public static List<LocalDate> getAllJingEnd() {
        List<String> allKeys = Arrays.asList(mmkv.allKeys());
        List<LocalDate> collect = allKeys.stream()
                .filter(key -> key != null && (key.startsWith("1") || key.startsWith("2")))
                .map(key -> LocalDate.parse(key))
                .sorted(Comparator.reverseOrder())
                .map(localDate -> {
                    return getDayNote(localDate);
                })
                .filter(dn -> dn.jingEnd == 1)
                .map(dayNote -> {
                    return LocalDate.parse(dayNote.date);
                })
                .collect(Collectors.toList());
        return collect;
    }


    //上一次经期开始
    public static LocalDate getLastJingStart() {
        String[] strings = mmkv.allKeys();
        if(strings==null) return null;

        List<String> allKeys = Arrays.asList(mmkv.allKeys());
        DayNote dayNote = allKeys.stream()
                .filter(key -> key != null && (key.startsWith("1") || key.startsWith("2")))
                .map(key -> LocalDate.parse(key))
                .sorted(Comparator.reverseOrder())
                .map(localDate -> {
                    return getDayNote(localDate);
                })
                .filter(dn -> dn.jingStart == 1)
                .findFirst()
                .orElse(null);

        if (dayNote == null) return null;
        return LocalDate.parse(dayNote.date);
    }

    //上一次经期结束
    public static LocalDate getLastJingEnd() {
        String[] strings = mmkv.allKeys();
        if(strings==null) return null;

        List<String> allKeys = Arrays.asList(mmkv.allKeys());
        DayNote dayNote = allKeys.stream()
                .filter(key -> key != null && (key.startsWith("1") || key.startsWith("2")))
                .map(key -> LocalDate.parse(key))
                .sorted(Comparator.reverseOrder())
                .map(localDate -> {
                    return getDayNote(localDate);
                })
                .filter(dn -> dn.jingEnd == 1)
                .findFirst()
                .orElse(null);

        if (dayNote == null) return null;
        return LocalDate.parse(dayNote.date);
    }


    public static class DayNote {

        public DayNote(String date) {
            this.date = date;
        }


        public String date;

        //经期开始 1 0
        public int jingStart;
        //经期结束 1 0
        public int jingEnd;

        //备注
        public String mark;
        //性行为 1 0
        public int haveSex;
        //是否保护措施
        public int tao;

        //次数
        public int sexCount;
        //高潮次数
        public int gaoCount;

        //性伙伴姓名
        public String partnerName;

        //心情
        public int xinQing;
        //经量
        public int jingLiang;
        //头晕
        public int touYun;
        //头痛
        public int touTong;
        //是否用药
        public int haveMedicine;
        //药品
        public String medicines;

        //体重
        public String weight;
        //体温
        public String temperature;

    }


}
